layui.use('layer', function() {
	var $ = layui.jquery,
		layer = layui.layer;
})

//添加内容
var addhtml = ''
addhtml += '<div class="form-div"><form id="fomr" class="layui-form" enctype="multipart/form-data">';
addhtml += '<div class="layui-form-item" style="width:500px;"><label class="layui-form-label">名称</label><div class="layui-input-block"><input type="text" name="title" autocomplete="off" class="layui-input" placeholder="请输入分类名称"></div></div>';
addhtml += '<div class="layui-form-item" style="width:500px;"><label class="layui-form-label">描述</label><div class="layui-input-block"><input type="text" name="description" autocomplete="off" class="layui-input" placeholder="请输入分类名称"></div></div>';
addhtml += '<input type="hidden" name="pid">';
addhtml += '<input type="hidden" name="token" value="' + token + '">';
addhtml += '</form></div>';

layui.use('element', function() {
	var element = layui.element;

	function list() {
		$.ajax({
			type: 'post',
			dataType: 'json',
			url: listSrc,
			success: function(data) {
				var code = data.code;
				var msg = data.msg;
				var data = data.data;
				if(code == 1) {
					for(var i = 0; i < data.length; i++) {
						var oHtml = '<div class="layui-colla-item"><h2 class="layui-colla-title">' + data[i].title + '</h2><div class="layui-colla-cz"><a class="iconfont tab-cz-icon zjicon" title="添加" data-id="' + data[i].id + '" onclick="Add($(this))"></a><a class="tab-cz-icon bjicon" title="编辑" data-id="' + data[i].id + '" onclick="edit($(this))"></a><a class="tab-cz-icon scicon" title="删除" data-tops="' + data[i].tops + '" data-id="' + data[i].id + '" onclick="del($(this))"></a></div><div class="layui-colla-content oHtml"></div></div>'
						$(".layui-collapse").append(oHtml);
						var oData = data[i].child;
						for(var j = 0; j < oData.length; j++) {
							var tHtml = '<div class="layui-colla-item"><h2 class="layui-colla-title">' + oData[j].title + '<div class="layui-colla-cz"><a class="iconfont tab-cz-icon zjicon" title="添加" data-id="' + oData[j].id + '" onclick="Add($(this))"></a><a class="tab-cz-icon bjicon" title="编辑" data-id="' + data[i].id + '" onclick="edit($(this))"></a><a class="tab-cz-icon scicon" title="删除" data-tops="' + oData[j].tops + '" data-id="' + oData[j].id + '" onclick="del($(this))"></a></div></h2><div class="layui-colla-content tHtml"></div></div>'
							$(".oHtml").eq(i).append(tHtml);
							var sData = oData[j].child;
							for(var k = 0; k < sData.length; k++) {
								var sHtml = '<div class="layui-colla-item"><h2 class="layui-colla-title">' + sData[k].title + '<div class="layui-colla-cz"><a class="tab-cz-icon bjicon" title="编辑" data-id="' + data[i].id + '" onclick="edit($(this))"></a><a class="tab-cz-icon scicon" title="删除" data-tops="' + sData[k].tops + '" data-id="' + sData[k].id + '" onclick="del($(this))"></a></div></h2></div>'
								$(".tHtml").eq(j).append(sHtml);
							}

						}
						element.init();
					}
				}

			}
		})
	}
	list();
})

function Add($this) {
	var id = $this.attr("data-id");
	layer.open({
		type: 1,
		title: "添加分类",
		area: ['600px', '240px'], //宽高
		maxmin: true,
		resize: true,
		content: addhtml,
		btn: ['提交', '关闭'],
		btn1: function() {
			var Adata = $("#fomr").serialize();
			$.ajax({
				type: 'post',
				dataType: 'json',
				url: addSrc,
				data: Adata,
				success: function(data) {
					var code = data.code;
					var msg = data.msg;
					if(code == 1) {
						layer.close(layer.index)
						layer.alert(msg, {
							icon: 1
						}, function() {
							location.reload();
						})
					} else {
						layer.alert(msg, {
							icon: 5
						})
					}
				}
			})
		}

	});

	$("#fomr input[name=pid]").val(id);

}

function edit($this){
	var id =$this.attr("data-id");
	$.ajax({
    	type:'post',
    	url:seeSrc,
    	data:{
    		id:id
    	},
    	success:function(data){
    		var code=data.code;
    		var msg=data.msg;
    			layer.open({
    			  title:"编辑货币",
				  type: 1,
				  skin: 'layui-layer-rim',
				  area: ['550px', '240px'],
				  content: addhtml,
				  btn: ['提交', '关闭'],
				  btn1: function(){
				  	var Adata=$("#fomr").serialize();
				    $.ajax({
				    	type:'post',
				    	dataType:'json',
				    	url:editSrc,
				    	data:Adata,
				    	success:function(data){
				    		var code=data.code;
				    		var msg=data.msg;
				    		if(code==1){
				    			layer.close(layer.index)
				    			layer.alert(msg,{icon: 1},function(){
				    				location.reload();
				    			})
				    		}else{
				    			layer.alert(msg,{icon: 5})
				    		}
				    	}
				    })
				  }
				});
				$("#fomr").formEdit(data);
    	}
    })
}

function del($this) {
	var id = $this.attr("data-id");
	var tops = $this.attr("data-tops");
	if(tops==0){
		var tsText='您确定要删除该分类！'
	}else{
		var tsText='该分类下有子分类，您确定要删除该分类！'
	}
	layer.alert(tsText, {
		icon: 2,
		btn: ['确定', '关闭'],
		btn1: function() {
			$.ajax({
				type: 'post',
				dataType: 'json',
				url: delSrc,
				data: {
					token: token,
					id: id
				},
				success: function(data) {
					var code = data.code;
					var msg = data.msg;
					if(code == 1) {
						layer.close(layer.index)
						layer.alert(msg, {
							icon: 1
						}, function() {
							location.reload();
						})
					} else {
						layer.close(layer.index)
						layer.alert(msg, {
							icon: 5
						})
					}
				}
			})
		}
	})
}